******************************
* Table 1- Balance table for estimation sample
* col 1, 2, 3, 4
************************
clear all
set trace off
set more off
use Jehanabad_analytic_sample_hemoglobin, replace
drop if wave==1

clear matrix
mat A1 = J(34,1,.)
mat A1a = J(34,1,.)
mat A2 = J(34,1,.)
mat A2a = J(34,1,.)
mat A3 = J(34,1,.)
mat A3a = J(34,1,.)
mat A4 = J(34,1,.)
mat A5 = J(34,1,.)
mat A6 = J(34,1,.)

local n 1

** Panel A- Child level outcome variables
* Anemia
foreach var in hemoglobin any_an mild_an mod_sev_an{
sum `var'
local mean = `r(mean)'
local sd = `r(sd)'
display `mean'
display `sd'
mat A1[`n',1] = `mean'
mat A1a[`n',1] = `sd'
local N = `r(N)'
display `N'
mat A4[`n',1] = `N'


ttest `var', by(treat)
local mean0 = `r(mu_1)'
display `mean0'
mat A2[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A2a[`n',1] = `sd0'
local mean1 = `r(mu_2)'
display `mean1'
mat A3[`n',1] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A3a[`n',1] = `sd1'

local diff `mean1' - `mean0'
display `diff'
mat A6[`n',1] = `diff'
reg `var' treat, vce(cluster school_id)
test treat
mat A5[`n',1] = `r(p)'

local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd
local sd0
local sd1
local mean
}

**Cognitive test outcomes

foreach var in block_design digit_span_forward digit_span_backward progressive_matrices day_and_night Cognition_Score_pca math reading total_attendance_2 {
sum `var'
local mean = `r(mean)'
local sd = `r(sd)'
display `mean'
display `sd'
mat A1[`n',1] = `mean'
mat A1a[`n',1] = `sd'
local N = `r(N)'
display `N'
mat A4[`n',1] = `N'


ttest `var', by(treat)
local mean0 = `r(mu_1)'
display `mean0'
mat A2[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A2a[`n',1] = `sd0'
local mean1 = `r(mu_2)'
display `mean1'
mat A3[`n',1] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A3a[`n',1] = `sd1'

local diff `mean1' - `mean0'
display `diff'
mat A6[`n',1] = `diff'
reg `var' treat, vce(cluster school_id)
test treat
mat A5[`n',1] = `r(p)'


local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd
local sd0
local sd1
local sddiff
local mean
}

** Panel B
* Demographics and socioeconomic variables
foreach var in muslim_HH sc_st block rural_HH no_HHmembers years_schooling_father years_schooling_mother asset_index sex_child {
sum `var'
local mean = `r(mean)'
local sd = `r(sd)'
display `mean'
display `sd'
mat A1[`n',1] = `mean'
mat A1a[`n',1] = `sd'
local N = `r(N)'
display `N'
mat A4[`n',1] = `N'


ttest `var', by(treat)
local mean0 = `r(mu_1)'
display `mean0'
mat A2[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A2a[`n',1] = `sd0'
local mean1 = `r(mu_2)'
display `mean1'
mat A3[`n',1] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A3a[`n',1] = `sd1'

local diff `mean1' - `mean0'
display `diff'
mat A6[`n',1] = `diff'
reg `var' treat, vce(cluster school_id)
test treat
mat A5[`n',1] = `r(p)'


local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd
local sd0
local sd1
local sddiff
local mean
}

* Health care / nutrition
foreach var in institut_delivery health_insurance diarrhea improved_sanitation diet_diversity_score no_meals cut_meals health_knowledge DDS_meat {
sum `var'
local mean = `r(mean)'
local sd = `r(sd)'
display `mean'
display `sd'
mat A1[`n',1] = `mean'
mat A1a[`n',1] = `sd'
local N = `r(N)'
display `N'
mat A4[`n',1] = `N'


ttest `var', by(treat)
local mean0 = `r(mu_1)'
display `mean0'
mat A2[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A2a[`n',1] = `sd0'
local mean1 = `r(mu_2)'
display `mean1'
mat A3[`n',1] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A3a[`n',1] = `sd1'

local diff `mean1' - `mean0'
display `diff'
mat A6[`n',1] = `diff'
reg `var' treat, vce(cluster school_id)
test treat
mat A5[`n',1] = `r(p)'


local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd
local sd0
local sd1
local sddiff
local mean
}


**Panel C
* School characteristics

collapse total_enrollment class_size student_teacher_ratio calories_ph iron_ph treat, by (school_id)
foreach var in total_enrollment class_size student_teacher_ratio {
sum `var'
local mean = `r(mean)'
local sd = `r(sd)'
display `mean'
display `sd'
mat A1[`n',1] = `mean'
mat A1a[`n',1] = `sd'
local N = `r(N)'
display `N'
mat A4[`n',1] = `N'


ttest `var', by(treat)
local mean0 = `r(mu_1)'
display `mean0'
mat A2[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A2a[`n',1] = `sd0'
local mean1 = `r(mu_2)'
display `mean1'
mat A3[`n',1] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A3a[`n',1] = `sd1'

local diff `mean1' - `mean0'
display `diff'
mat A6[`n',1] = `diff'
reg `var' treat, vce(cluster school_id)
test treat
mat A5[`n',1] = `r(p)'


local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd
local sd0
local sd1
local mean
}


matrix rownames A4 = "Hemoglobin" "Any anemia" "Mild anemia" "Moderate or severe anemia" "Block design" "Digit span forward" "Digit span backward" "Progressive matrices" "Day and night" "Cognition score index" "Math" "Reading" "School attendance" "Muslim HH" "Sc/st" "Block" "Rural HH" "No of HH members" "Years schooling father" "Years schooling mother" "Asset index" "Male child" "Institutional delivery" "Health insurance" "Diarrhea" "Improved sanitation" "Diet diversity score" "Number of meals" "Cut meals" "Maternal health knowledge" "Child eats meat products"  "Total enrollment" "Class size" "Student teacher ratio"            
frmttable , statmat(A4) replace sdec(0) title (Table 1: Baseline summary statistics  for education sample (balance check)) ctitles("", " N (1)") dwide note("This table presents summary statistics as well as p-values for the difference in the means t-tests between children in the treatment and the control schools. All variables shown are child-level characteristics from the baseline except for panel C, which shows school-level variables. SD: Standard deviation; SC/ST: Scheduled Caste/Scheduled Tribe are the social and economically disadvantaged groups; N: Number of observations. One school was dropped from the control group between the survey period. ")  
frmttable , statmat(A2) replace sdec(2) merge ctitles(" Control mean (2)")
frmttable , statmat(A2a) replace sdec(2) merge ctitles(" Control (SD)")  
frmttable , statmat(A3) replace sdec(2) merge ctitles("Treatment mean (3) ") 
frmttable , statmat(A3a) replace sdec(2) merge ctitles(" Treatment (SD)") 
frmttable , statmat(A5) replace sdec(3) merge ctitles("p-values (T-C) (4) ")  


frmttable using table1_balance_healthsample.doc, replay replace

*****************************
*** generate col(5-8), Table 1 **
*********************************
use Jehanabad_analytic_sample_hemoglobin_attrition_sample.dta

run table_1.do on this sample to generate col 5-8 of Table 1


